<?php
/**
 *
 *
 */
include_once('User.php');
include_once('db_connect.php');

class UserDetail {

	private $userDetailID;
	private $userID;
	private $desYourself;
	private $firstName;
	private $lastName;
	private $birthDay;
	private $gender;
	private $nickName;
	private $add;
	private $shelterAdd;
	private $highSchool;
	private $university;
	private $tel;
	private $email;
	private $destroy;

	function UserDetail() {
		$this->userID = 0;
		$this->desYourself = "";
		$this->firstName = "";
		$this->lastName = "";
		$this->birthDay = "";
		$this->gender = 0;
		$this->nickName = "";
		$this->add = "";
		$this->shelterAdd = "";
		$this->highSchool = "";
		$this->university = "";
		$this->tel = "";
		$this->email = "";
		$this->destroy = 0;
	}

	// Connect to database
	function connect() {
		$this->connect = new Connect();
		$this->mysqli = $this->connect->getMysqli();
	}

	function getUserDetailID() {
		return $this->userDetailID;
	}

	function setUserDetailID($value) {
		$this->userDetailID = $value;
	}

	function getUserID() {
		return $this->userID;
	}

	function setUserID($value) {
		$this->userID = $value;
	}

	function getDesYourSelf() {
		return $this->desYourself;
	}

	function setDesYourSelf($value) {
		$this->desYourself = $value;
	}

	function getFirstName() {
		return $this->firstName;
	}

	function setFirstName($value) {
		$this->firstName = $value;
	}

	function getLastName() {
		return $this->lastName;
	}

	function setLastName($value) {
		$this->lastName = $value;
	}

	function getBirthDay() {
		return $this->birthDay;
	}

	function setBirthDay($value) {
		$this->birthDay = $value;
	}

	function getGender() {
		return $this->gender;
	}

	function setGender($value) {
		$this->gender = $value;
	}

	function getNickName() {
		return $this->nickName;
	}

	function setNickName($value) {
		$this->nickName = $value;
	}

	function getAdd() {
		return $this->add;
	}

	function setAdd($value) {
		$this->add = $value;
	}

	function getShelterAdd() {
		return $this->shelterAdd;
	}

	function setShelterAdd($value) {
		$this->shelterAdd = $value;
	}

	function getHighSchool() {
		return $this->highSchool;
	}

	function setHighSchool($value) {
		$this->highSchool = $value;
	}

	function getUniversity() {
		return $this->university;
	}

	function setUniversity($value) {
		$this->university = $value;
	}

	function getTel() {
		return $this->tel;
	}

	function setTel($value) {
		$this->tel = $value;
	}

	function getEmail() {
		return $this->email;
	}

	function setEmail($value) {
		$this->email = $value;
	}

	function getDestroy() {
		return $this->destroy;
	}

	function setDestroy($value) {
		$this->destroy = $value;
	}

	function insert($userDetail) {

		$userID 		= $userDetail->getUserID();
		$desYourself 	= $userDetail->getDesYourSelf();
		$firstName 		= $userDetail->getFirstName();
		$lastName 		= $userDetail->getLastName();
		$birthDay 		= $userDetail->getBirthDay();
		$gender 		= $userDetail->getGender();
		$nickName 		= $userDetail->getNickName();
		$add 			= $userDetail->getAdd();
		$shelterAdd 	= $userDetail->getShelterAdd();
		$highSchool 	= $userDetail->getHighSchool();
		$university 	= $userDetail->getUniversity();
		$tel 			= $userDetail->getTel();
		$email 			= $userDetail->getEmail();

		//Connect to database.
		$this->connect();

		// sql query Insert
		$sql = "INSERT INTO USERDETAIL 
				(UserID, DesYourSelf, FirstName, LastName,
					BirthDay, Gender, NickName, Address, ShelterAdd,
					HighSchool, University, Tel, Email)
				VALUES 
				(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

		// Prepare statement.
		$prInsert = $this->mysqli->prepare($sql);

		// Check
		if($prInsert) {

			// Bind
			$prInsert->bind_param('issssisssssss',
									$userID,
									$desYourself,
									$firstName,
									$lastName,
									$birthDay,
									$gender,
									$nickName,
									$add,
									$shelterAdd,
									$highSchool,
									$university,
									$tel,
									$email
									);
		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. "): " .$this->mysqli->error;
		}

		// Execute
		if(!$prInsert->execute()) {
			echo "ERROR excute: (" .$prInsert->errno. "): " .$prInsert->error;
		}
		return true;
	}

	/**
	 *	Function getUserDetail($userID)
	 *	return UserDetail Object
	 */
	function getUserDetail($userID) {

		$this->connect();

		// sql query
		$sql = "SELECT * FROM USERDETAIL WHERE UserID = ?";
		// prepare sql query.
		$prSelect = $this->mysqli->prepare($sql);

		// Check sql query was prepared
		if($prSelect) {
			$prSelect->bind_param('i', $userID);

		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. ") " .$this->mysqli->error;
		}

		//Execute
		if(!$prSelect->execute()) {
			echo "ERROR execute: (".$prSelect->errno.") ".$prSelect->error;
		}

		// Get result os query.
		$result = $prSelect->get_result();
		$row = $result->fetch_assoc();

		$desYourself = $row['DesYourself'];
		$firstName = $row['FirstName'];
		$lastName = $row['LastName'];
		$birthDay = $row['Birthday'];
		$gender = $row['Gender'];
		$nickName = $row['NickName'];
		$address = $row['Address'];
		$shelterAdd = $row['ShelterAdd'];
		$highSchool = $row['HighSchool'];
		$university = $row['University'];
		$tel = $row['Tel'];
		$email = $row['Email'];

		// Get value of execute sql query
		// Set value of $userDetail
		$userDetail = new UserDetail();

		$userDetail->setUserID($userID);
		$userDetail->setDesYourSelf($desYourself);
		$userDetail->setFirstName($firstName);
		$userDetail->setLastName($lastName);
		$userDetail->setBirthDay($birthDay);
		$userDetail->setGender($gender);
		$userDetail->setNickName($nickName);
		$userDetail->setAdd($address);
		$userDetail->setShelterAdd($shelterAdd);
		$userDetail->setHighSchool($highSchool);
		$userDetail->setUniversity($university);
		$userDetail->setTel($tel);
		$userDetail->setEmail($email);

		// return $userDetail
		return $userDetail;
	}

	function Update($userDetail) {

		$this->connect();

		$userID = $userDetail->getUserID();
		$desYourSelf = $userDetail->getDesYourSelf();
		$firstName = $userDetail->getFirstName();
		$lastName = $userDetail->getLastName();
		$birthDay = $userDetail->getBirthDay();
		$gender = $userDetail->getGender();
		$nickName = $userDetail->getNickName();
		$add = $userDetail->getAdd();
		$shelterAdd = $userDetail->getShelterAdd();
		$highSchool = $userDetail->getHighSchool();
		$university = $userDetail->getUniversity();
		$tel = $userDetail->getTel();
		$email = $userDetail->getEmail();
		$destroy = $userDetail->getDestroy();

		// sql query
		$sql = "UPDATE USERDETAIL
				SET
					DesYourself = ?,
					FirstName = ?,
					LastName = ?,
					BirthDay = ?,
					Gender = ?,
					NickName = ?,
					Address = ?,
					ShelterAdd = ?,
					HighSchool = ?,
					University = ?,
					Tel = ?,
					Email = ?,
					Destroy = ?
				WHERE
					UserID = ?";

		// prepare sql query.
		$prSelect = $this->mysqli->prepare($sql);

		// Check sql query was prepared
		if($prSelect) {
			$prSelect->bind_param('ssssisssssisii', 
								$desYourSelf,
								$firstName,
								$lastName,
								$birthDay,
								$gender,
								$nickName,
								$add,
								$shelterAdd,
								$highSchool,
								$university,
								$tel,
								$email,
								$destroy,
								$userID
								);

		} else {
			echo "Prepare failed: (" .$this->mysqli->errno. ") " .$this->mysqli->error;
		}

		//Execute
		if(!$prSelect->execute()) {
			echo "ERROR execute: (".$prSelect->errno.") ".$prSelect->error;
		}

		return true;
	}
}
?>